import uicls
import blue
import uiconst
import uix
import form
import uthread
import uiutil

class WindowStack(uicls.WindowStackCore):
    __guid__ = 'uicls.WindowStack'

    def Check(self, updatewnd = 0, autoselecttab = 1, checknone = 0):
        if ((self is None) or self.destroyed):
            return 
        if (checknone and (len(self.sr.content.children) == 0)):
            sm.GetService('window').UnregisterStack(self.name)
            self.SelfDestruct()
            return 
        self.SetMinWH()
        tabs = []
        label = ''
        someoneunkillable = 0
        allLite = True
        for wnd in self.sr.content.children:
            if ((wnd is None) or wnd.destroyed):
                continue
            tabs.append([wnd.GetCaption(),
             wnd,
             self,
             wnd])
            wnd.HideHeader()
            wnd.HideBackground()
            if not wnd._pinned:
                allLite = False
            wnd.state = uiconst.UI_PICKCHILDREN
            label = ((label + wnd.GetCaption()) + '-')
            if wnd._killable:
                continue
            someoneunkillable = 1
            continue

        sm.GetService('window').ToggleLiteWindowAppearance(self, allLite)
        if someoneunkillable:
            self.MakeUnKillable()
        else:
            self.MakeKillable()
        if len(tabs):
            if len(label):
                label = label[:-1]
            uix.Flush(self.sr.tabs)
            maintabs = form.TabGroup(uicls.Container(parent=self.sr.tabs, name='tabparent', align=uiconst.TOTOP, height=18)).Startup(tabs, self.name, None, 0, 0, autoselecttab)
            maintabs.rightMargin = 80
            for i in xrange(len(maintabs.sr.tabs)):
                tab = maintabs.sr.tabs[i]
                wnd = self.sr.content.children[i]
                tab.GetMenu = wnd.GetMenu
                tab.SetIcon(wnd.headerIconNo, 14, getattr(wnd.sr.headerIcon, 'hint', ''), getattr(wnd.sr.headerIcon, 'GetMenu', None))

            self.SetCaption(label)



    def OnPin_(self, *args):
        for wnd in self.sr.content.children:
            if ((wnd is None) or wnd.destroyed):
                continue
            wnd._pinned = self._pinned




    def OnUnpin_(self, *args):
        for wnd in self.sr.content.children:
            if ((wnd is None) or wnd.destroyed):
                continue
            wnd._pinned = self._pinned




    def RemoveWnd(self, wnd, grab, correctpos = 1, idx = 0, dragging = 0, check = 1):
        if (wnd.parent != self.sr.content):
            return 
        if hasattr(wnd, 'OnTabSelect'):
            uthread.worker('WindowStack::RemoveWnd', wnd.OnTabSelect)
        if self._pinned:
            sm.GetService('window').ToggleLiteWindowAppearance(wnd, True)
        wnd._detaching = True
        uiutil.Transplant(wnd, self.parent, idx)
        wnd.sr.stack = None
        wnd.sr.tab = None
        wnd.align = uiconst.UI_ALNONE
        wnd.state = uiconst.UI_NORMAL
        wnd.grab = grab
        wnd.width = (wnd._fixedWidth or self.width)
        wnd.height = (wnd._fixedHeight or self.height)
        if dragging:
            uthread.new(wnd.BeginDrag)
        wnd.ShowHeader()
        wnd.ShowBackground()
        wnd.sr.resizers.state = uiconst.UI_PICKCHILDREN
        if correctpos:
            wnd.left = (uicore.uilib.x - grab[0])
            wnd.top = (uicore.uilib.y - grab[1])
        if check:
            self.Check()
        wnd.RegisterStackID()
        wnd._detaching = False
        wnd._dragging = dragging
        if ((len(self.sr.content.children) == 1) and (not self._collapsed)):
            w = self.sr.content.children[0]
            (aL, aT, aW, aH,) = self.GetAbsolute()
            (x, y,) = (aL, aT)
            self.RemoveWnd(w, (0, 0), 1, 1, check=0)
            (w.left, w.top,) = (x, y)
            return 
        if (len(self.sr.content.children) == 0):
            self.SelfDestruct()



    def Detach(self, wnd, grab):
        if (settings.user.windows.Get('lockwhenpinned', 0) and self._pinned):
            return 0
        else:
            return uicls.WindowStackCore.Detach(self, wnd, grab)




